package proj2;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.math.*;
/**
 * PI1 is the default method for calculating Pi
 * @author Yerong
 */
public class PI1 {
    // result is the approximation result
    private BigDecimal result;
    public PI1()
    {
      result=new BigDecimal(0);
    }
    public BigDecimal calculate(int n)
    {
        int it= 0;
        // term is the indivial term in the summation
        BigDecimal term;
        for (it = 0; it < n; it++) {
            term = BigDecimal.ONE; 
            term = term.divide(new BigDecimal(2*it+1), 100, RoundingMode.CEILING);        
            if (1 == it%2)
                term = term.multiply(new BigDecimal(-1));
            result = result.add(term);
        }
        result = result.multiply(new BigDecimal(4));
        return result;
    }
}
